package com.un.ebs.product.mapper;

import com.un.ebs.product.domain.Product;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author liubo
 * @since 2020-02-16
 */
@Component
public interface ProductMapper extends BaseMapper<Product> {

    @Update("<script>" +
            "update tb_product set unit_type=#{unitType},sale_type=#{saleType},is_deliver_flow=#{isDeliverFlow},is_deliver_audit_status=#{isDeliverAudit} where class_id in " +
            "<foreach collection='classIds' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    void updateBatch(@Param("classIds") String[] classIds, @Param("unitType") int unitType, @Param("saleType") int saleType, @Param("isDeliverFlow") boolean isDeliverFlow, @Param("isDeliverAudit") boolean isDeliverAudit);

    @Select("select isnull(max(sort)+1,1) from tb_product where class_id=#{classId}")
    int maxSortByClassId(@Param("classId") String classId);

    @Select("select count(*) from tb_product where code=#{code} and is_delete=0")
    int selectCountByCode(@Param("code") String code);

    @Select("select count(id) from tb_product where code = #{code} and is_delete=0 and id <> #{productId}")
    int selectCount2ByCode(@Param("code") String code,@Param("productId") String productId);

    @Select("select count(*) from tb_product_sku where is_delete=0 and product_id=#{productId}")
    int selectSkuCountBySpuId(@Param("productId") String productId);

    @Select("SELECT COUNT(id) FROM dbo.tb_inventory WHERE sku_id IN (SELECT id FROM dbo.tb_product_sku WHERE product_id=#{productId})")
    int selectInventoryQty(@Param("productId") String productId);

    @Update("update tb_product_sku set sku_code=#{code} where product_id=#{productId}")
    int updateSku(@Param("productId") String productId,@Param("code") String code);

    @Select("select * from v_product_ext where id=#{id}")
    Product selectDeleteById(@Param("id") String id);

    @Update("update tb_product set is_delete=0 where id=#{id}")
    void undelete(@Param("id") String id);
}
